package server.business.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import server.business.mapper.BloodPriceMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import server.business.pojo.BloodPrice;
import server.business.service.BloodPriceService;
import server.business.vo.Result;
import server.business.vo.ResultExpand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author FZX
 * @since 2022-08-06
 */
@Service
public class BloodPriceServiceImpl extends ServiceImpl<BloodPriceMapper, BloodPrice> implements BloodPriceService {

    @Resource
    private BloodPriceMapper bloodPriceMapper;
    
    @Autowired
    private BloodPriceService bloodPriceService;

    @Override
    public List<BloodPrice> listBloodPrice() {
        return this.bloodPriceMapper.selectList(new QueryWrapper<>());
    }

    public IPage<BloodPrice> selectByPage(int currentPage, int pageSize){
        Page<BloodPrice> page = new Page<>(currentPage, pageSize);
        IPage<BloodPrice> bloodPriceIPage = bloodPriceMapper.selectPage(page, null);
        return bloodPriceIPage;
    }

    public ResultExpand getPage(HttpServletRequest request, HttpServletResponse response){
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        IPage<BloodPrice> bloodPriceIPage = bloodPriceService.selectByPage(currentPage, pageSize);
        ResultExpand resultExpand = new ResultExpand("200", "成功", bloodPriceIPage.getRecords(), currentPage, pageSize, bloodPriceIPage.getTotal(), bloodPriceIPage.getPages());
        response.setStatus(200);
        return resultExpand;
    }

    public Result getBloodPrice() {
        List<BloodPrice> bloodList= bloodPriceService.listBloodPrice();
        Result result = new Result();
        result.setCode("200").setMsg("查询血液价格成功").setData(bloodList);
        return result;
    }

    public ResultExpand searchPrice(HttpServletRequest request, HttpServletResponse response){
        String bloodType = request.getParameter("bloodType");
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
//        IPage<BloodPrice> page = new Page<>(currentPage, pageSize);
      
        QueryWrapper<BloodPrice> queryWrapper = new QueryWrapper<>();
        
        if(bloodType != "" && bloodType != null)
            queryWrapper.eq("blood_type", bloodType);

        IPage<BloodPrice> page = bloodPriceMapper.selectBloodPricePage(
                new Page<>(currentPage, pageSize),  queryWrapper
        );
//        bloodPriceService.page(page, queryWrapper);
        
        ResultExpand resultExpand = new ResultExpand();
        resultExpand.setCurrent(currentPage).setPageSize(pageSize).setTotal(page.getTotal()).setPages(page.getPages());
        resultExpand.setCode("200");
        resultExpand.setMsg("搜索成功");
        resultExpand.setData(page.getRecords());
        return resultExpand;
    }

    public void downloadExcel(HttpServletRequest request, HttpServletResponse response){
        String bloodType = request.getParameter("bloodType");
        try {
            String filename = "血液价格";
            String userAgent = request.getHeader("User-Agent");
            if(userAgent.contains("MSIE")||userAgent.contains("Trident")){
                filename = URLEncoder.encode(filename,"UTF-8");            }else {
                filename = new String(filename.getBytes("UTF-8"),"ISO-8859-1");
            }
            response.setContentType("application/json.ms-exce");
            response.setCharacterEncoding("utf-8");
            response.addHeader("Content-Disposition","filename = " + filename + ".xlsx");
            QueryWrapper<BloodPrice> queryWrapper = new QueryWrapper<>();
            if(bloodType != "" && bloodType != null)
                queryWrapper.le("blood_type", bloodType);
            List<BloodPrice> bloodPriceList = bloodPriceService.list(queryWrapper);
            EasyExcel.write(response.getOutputStream(),BloodPrice.class).sheet("sheet").doWrite(bloodPriceList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
